package com.ete.service.net.progress

import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * Author:LiXian
 * <p>
 * Desc:上传进度
 * Version:1.0
 * date 2024/1/9 16:59
 */
class ProgressInterceptor(
        private var mListener: IProgressListener? = null,
        private var mProgressKey: String? = null
) : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest: Request = chain.request()
        return if (mListener == null || originalRequest.body() == null) {
            chain.proceed(originalRequest)
        } else {
            val progressRequest: Request = originalRequest.newBuilder()
                .method(originalRequest.method(), ProgressRequestBody(originalRequest.body(), mListener, mProgressKey))
                .build()
            chain.proceed(progressRequest)
        }
    }
}